package com.iwhalecloud.citybrain.flow.platform.manager.application.service.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.context.FlowElementProcessContext;
import com.iwhalecloud.citybrain.flow.platform.manager.application.service.CustomPropertiesProcessor;
import com.iwhalecloud.citybrain.flow.platform.manager.domain.repository.FlowReTimeoutSettingRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 处理配置的通知渠道等属性
 * <p>
 * properties -> noticeChannel
 * -> candidateUsers
 *
 * @author lilh
 * @date 2020/11/16 16:47
 */
@Service
public class ActivityNoticeCustomPropertiesProcessor implements CustomPropertiesProcessor {

    @Autowired
    private FlowReTimeoutSettingRepository flowReTimeoutSettingRepository;

    @Override
    public boolean support(JsonNode elementNode) {
        JsonNode properties = elementNode.get("properties");
        if (nonNull(properties)) {
            JsonNode candidateUsers = properties.get("candidateUsers");
            JsonNode noticeChannel = properties.get("noticeChannel");
            return nonNull(candidateUsers) && nonNull(noticeChannel);
        }
        return false;
    }

    @Override
    public void process(FlowElementProcessContext context) {
        flowReTimeoutSettingRepository.saveActivityNoticeChannel(context.getDeployId(), context.getNodeCode(),
                context.getElementNode().get("properties").get("candidateUsers"),
                context.getElementNode().get("properties").get("noticeChannel"));
    }
}
